window.addEvent('domready',function(){
	/**
	 * Scroller 
	 */
	var caruselContainer = $('carusel-container');
	var frontOffer = $('front-offer');

	var storeSlide = new Hash();
	
	frontOffer.getElements('a').addEvent('click', function(e) {

		// usuwanie zaznaczeni starego
		this.getParent('li').getAllPrevious().removeClass('active');
		this.getParent('li').getAllNext().removeClass('active');
		// aktualne zaznaczenie
		this.getParent('li').addClass('active');

		caruselContainer.getElements('div[id^=album-container]').setStyle('display','none');
		
		var href = this.get('href');

		var divCarusel = caruselContainer.getElement(href);
		divCarusel.setStyle('display','block');

		// inicjowanie tylko 1 raz
		if (!storeSlide.has(href)) {
			storeSlide.set(href,true);

			// opuźnienie dla 'false' .. bo też e.stop() nie działa :/..
			setTimeout(function() {
				var nS1 = new noobSlide({
					box: divCarusel.getElement('.carusel-box'),
					items: divCarusel.getElements('img'),
					size: 600,
					autoPlay: true,
					interval: 3500,
					addButtons: {
						previous: divCarusel.getElement('.prev'),
						next: divCarusel.getElement('.next')
					}
				});
			}, 200);
		}		

		return false;
	});
	
	// odpalenie scrollera
	frontOffer.getElement('.active a').fireEvent('click');
	
	/**
	 * Dba o linki 
	 */
	
	var content = $('content');
	content.getElements('a').each(function(i){
		var item = $(i); 
		item.set('href', item.get('href') + '#main');
	});
	
	content.getElements('[rel*=lightbox]').slimbox({/* Put custom options here */}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
	
	/**
	 * Menu nawigacyjne 
	 */
	var navEls = $$('#main .navigation li a');
	navEls.addEvent('click', function(e) {
		if (this.getParent('li').hasClass('gallery-menu')) {
			return true;
		}

		// usuwanie zaznaczeni starego
		this.getParent('li').getAllPrevious().removeClass('active');
		this.getParent('li').getAllNext().removeClass('active');
		// aktualne zaznaczenie
		this.getParent('li').addClass('active');

		var href = this.get('href') + '/format/html';
		href = href.replace('#main','');
		href += '#main';
		
		var myHTMLRequest = new Request.HTML({
			onRequest: function(){
				content.fade(0.3);
			},
			onSuccess: function(responseTree, responseElements, responseHTML){
				async: true,
				content.set('html',responseHTML);
				content.getElements('a')
					.addEvent('click', function() {
						// jest to link http:// 
						if (this.rel != undefined && this.rel == 'noajax') {
							return true;
						} else
						// jest to galeria
						if (this.href.replace(/gallery\/(gallery|album|image)/i,'') != this.href) {
							return true;
						} else
						// nie jest to rozszerzenie gif,jpg,png
						if (this.href.replace(/\.(jpg|gif|png)(#main|)$/i,'') != this.href) {
							myHTMLRequest.get(this.href.replace('#main','') + '/format/html');
							return false;
						} else {
							//console.log(this.href.replace(/\.(jpg|gif|png)(#main|)$/i,''));
						}
					})
					.filter(function(el) {
						return !el.href.replace(/\.(jpg|gif|png)(#main|)$/i,'') == el.href;
					})
					.slimbox({/* Put custom options here */}, null, function(el) {
						return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
					});
				content.fade(1);
			},
			onFailure: function() {
				content.fade(1);
				document.location.href = href;
			}
		});
		myHTMLRequest.get(href);

		return false;
	});
});